const util = require('../../utils/util');
const API = require('../../common/constant');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    events: [],
    pageIndex: 1,
    pageCount: 0
  },
  /**
   * 修改数据
   */
  changeData: function (data) {
    for (let i = 0; i < data.length; i++) {
      if (data[i].eventType === "chat") {
        data[i].activityType = "#解闷聊天#";
        data[i].activityColor = '#F4BC11';
      } else if (data[i].eventType === "partner") {
        data[i].activityType = "#美食搭伙#";
        data[i].activityColor = '#FF8023';
      } else if (data[i].eventType === 'find') {
        data[i].activityType = "#求邂逅#";
        data[i].activityColor = '#FE3232';
      }
    }
    console.log(data);
    return data;
  },
  /**
   * 获取发布的活动
   */
  getPublished: function () {
    util.http({
      url: API.URL.EVENT.FIND_EVENTCOLLECTION,
      data: {
        pageIndex: this.data.pageIndex,
        pageSize: 4
      },
      success: res => {
        console.log(res.data.bussData);
        let newData = this.changeData(res.data.bussData);
        this.setData({
          events: this.data.events.concat(newData),
          pageCount: res.data.pageCount
        })
      },
      fail: rej => {
        console.log(rej);
      }
    })
  },
  /**
   * 跳到活动管理页面
   */
  toEventManage: function (e) {
    wx.navigateTo({
      url: '../FanInfo/FanInfo?id=' + e.currentTarget.id + '&status=' + e.currentTarget.dataset.hi
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPublished();
    app.globalData.isNewPublish = false;    
  },
  onShow: function(){
    if (app.globalData.isNewCollect){
      this.setData({
        events: [],
        pageIndex: 1,
      })
      this.getPublished();
      app.globalData.isNewPublish = false;
    }
  },
  onReachBottom() {
    if (this.data.pageCount <= this.data.pageIndex) return
    this.setData({
      pageIndex: this.data.pageIndex + 1
    })
    this.getPublished();
  },
  
})